< previous page page_451 next page >

Page 451
9f31265bb40b592b1929e18a47ab28e5.gif
      If oEmp Is Nothing Then
         MsgBox "Employee ID Not Found"
      Else
         ' Display the Employee Name
         oEmp.EmployeeName
      End If
   Else
      MsgBox "No Employees Have Been Added"
   End If
End Sub
In the code for the Click event, youll see that you need to dim an Employee object. Dont dim a new Employee object, just dim a pointer to an existing object. Remember that the objects already exist in the collectionyoure just retrieving a pointer to one of those objects.
After declaring the Employee object, make sure there are actually employees in the Employees class by checking the Count property. If there are employees, go ahead and call the Item method, passing in the value in the text box txtSearch. After the call to the Item method, you should check to see whether the oEmp variable is equal to Nothing. If it is, you know that the employee you were looking for was not found. If the employee was found, you can invoke the EmployeeName method on the returned Employee object to display that employee. This is now a very robust implementation for using collections.
Adding Iteration
By default, any collection class you create does not support the ability to iterate through the collection using the For Each...Next construct. You can add this ability, but you must set a special magic number in the procedure attributes dialog box.
Examples Filename: Emp4.frm
Consider the example shown in Figure 22.5, where there are two command buttons that have two different methods for looping through the values contained in the Employees collection class.

 
< previous page page_451 next page >

If you like this book, buy it!